/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import bean.Assurance;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import bean.Vehicule;
import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author mezouari
 */
public class AssuranceJpaController implements Serializable {

    public AssuranceJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Assurance assurance) throws IllegalOrphanException {
        List<String> illegalOrphanMessages = null;
        Vehicule immatriculationOrphanCheck = assurance.getImmatriculation();
        if (immatriculationOrphanCheck != null) {
            Assurance oldAssuranceOfImmatriculation = immatriculationOrphanCheck.getAssurance();
            if (oldAssuranceOfImmatriculation != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Vehicule " + immatriculationOrphanCheck + " already has an item of type Assurance whose immatriculation column cannot be null. Please make another selection for the immatriculation field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Vehicule immatriculation = assurance.getImmatriculation();
            if (immatriculation != null) {
                immatriculation = em.getReference(immatriculation.getClass(), immatriculation.getId());
                assurance.setImmatriculation(immatriculation);
            }
            em.persist(assurance);
            if (immatriculation != null) {
                immatriculation.setAssurance(assurance);
                immatriculation = em.merge(immatriculation);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Assurance assurance) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Assurance persistentAssurance = em.find(Assurance.class, assurance.getId());
            Vehicule immatriculationOld = persistentAssurance.getImmatriculation();
            Vehicule immatriculationNew = assurance.getImmatriculation();
            List<String> illegalOrphanMessages = null;
            if (immatriculationNew != null && !immatriculationNew.equals(immatriculationOld)) {
                Assurance oldAssuranceOfImmatriculation = immatriculationNew.getAssurance();
                if (oldAssuranceOfImmatriculation != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Vehicule " + immatriculationNew + " already has an item of type Assurance whose immatriculation column cannot be null. Please make another selection for the immatriculation field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (immatriculationNew != null) {
                immatriculationNew = em.getReference(immatriculationNew.getClass(), immatriculationNew.getId());
                assurance.setImmatriculation(immatriculationNew);
            }
            assurance = em.merge(assurance);
            if (immatriculationOld != null && !immatriculationOld.equals(immatriculationNew)) {
                immatriculationOld.setAssurance(null);
                immatriculationOld = em.merge(immatriculationOld);
            }
            if (immatriculationNew != null && !immatriculationNew.equals(immatriculationOld)) {
                immatriculationNew.setAssurance(assurance);
                immatriculationNew = em.merge(immatriculationNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = assurance.getId();
                if (findAssurance(id) == null) {
                    throw new NonexistentEntityException("The assurance with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Assurance assurance;
            try {
                assurance = em.getReference(Assurance.class, id);
                assurance.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The assurance with id " + id + " no longer exists.", enfe);
            }
            Vehicule immatriculation = assurance.getImmatriculation();
            if (immatriculation != null) {
                immatriculation.setAssurance(null);
                immatriculation = em.merge(immatriculation);
            }
            em.remove(assurance);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Assurance> findAssuranceEntities() {
        return findAssuranceEntities(true, -1, -1);
    }

    public List<Assurance> findAssuranceEntities(int maxResults, int firstResult) {
        return findAssuranceEntities(false, maxResults, firstResult);
    }

    private List<Assurance> findAssuranceEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Assurance.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Assurance findAssurance(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Assurance.class, id);
        } finally {
            em.close();
        }
    }

    public int getAssuranceCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Assurance> rt = cq.from(Assurance.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
